/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lanchat.model.util;

import com.lanchat.model.data.LogInfo;
import com.lanchat.model.data.User;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 存取聊天记录
 * 
 * @author zhangyiheng
 */
public class LocalSaver {
    private static final String LOGFILE=".\\log.dat";
    private ObjectInputStream in;
    private LogObjectOutputStream out;
    private File f;
    private ArrayList<LogInfo> infoList;

    public LocalSaver(){
        try {
            f = new File(LOGFILE);
            if(!f.exists()){
                f.createNewFile();
                FileOutputStream fout=new FileOutputStream(f,true);
                out =LogObjectOutputStream.NewInstanse(f, new FileOutputStream(f,true));
                User u=new User();
                u.setUserName("abc");
                u.setAddress(LocalIPGetter.getMyIP());
                LogInfo info=new LogInfo("msg", new Date(), u);
                out.writeObject(info);
                out.close();
            }
            infoList=new ArrayList<LogInfo>();
            FileInputStream fin=new FileInputStream(f);
            in = new ObjectInputStream(fin);
        } catch (Exception ex) {
            Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public LogInfo ReadMessage(){
        LogInfo res=null;
            try {
                res =(LogInfo)in.readObject();
            } catch (IOException ex) {
                Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
            }
        return res;
    }

    public void SaveMessage(LogInfo msg){
        try {
            out=LogObjectOutputStream.NewInstanse(f, new FileOutputStream(f, true));
            out.writeObject(msg);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ReadList(){
         
            try {
                in = new ObjectInputStream(new FileInputStream(f));
            } catch (IOException ex) {
                Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
            }
        LogInfo tmp=null;
        try {
            tmp = (LogInfo) in.readObject();
        } catch (IOException ex) {
            Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
        }
        while(tmp!=null){
            this.infoList.add(tmp);
            try {
                tmp = (LogInfo) in.readObject();
            } catch (Exception ex) {
                tmp=null;
            }
            if(tmp==null){
                break;
            }           
        }
    }

    
    public void dispose(){
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(LocalSaver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the infoList
     */
    public ArrayList<LogInfo> getInfoList() {
        return infoList;
    }
  
}
